<?php
namespace api\modules\v1\controllers;

use api\components\ApiController;
use api\modules\v1\models\Basic;
use Yii;

/**
 * 国家、城市、银行控制器
 * @author WJ
 */
class BasicController extends ApiController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['authenticator']['optional'] = ['country-list', 'province-list', 'city-list', 'bank-list'];
        return $behaviors;
    }

    /**
     * 国家列表
     * @author wj
     */
    public function actionCountryList()
    {
        $model = new Basic();
        if ($result = $model->getCountry()) {
            return ['message' => '国家列表获取成功', 'data' => $result];
        }
    }

    /**
     * 省份列表
     * @author wj
     */
    public function actionProvinceList()
    {
        $params = Yii::$app->request->post();
        $model = new Basic();
        $model->setScenario(Basic::SCENARIO_PROVINCE_LIST);
        $model->load($params, '');
        if ($result = $model->getProvince()) {
            return ['message' => '省份列表获取成功', 'data' => $result];
        }else{
            return ['message' => '省份列表获取成功', 'data' => []];
        }
    }

    /**
     * 获取城市、区域
     * @author wj
     */
    public function actionCityList()
    {
        $params = Yii::$app->request->post();
        $model = new Basic();
        $model->setScenario(Basic::SCENARIO_CITY_LIST);
        $model->load($params, '');
        if ($result = $model->getCity()) {
            return ['message' => '城市列表获取成功', 'data' => $result];
        }
    }

    /**
     * 银行列表
     * @author wj
     */
    public function actionBankList()
    {
        $model = new Basic();
        if ($result = $model->getBank()) {
            return ['message' => '银行列表获取成功', 'data' => $result];
        }
    }
}
